/* Copyright (c) Colorado School of Mines, 2010.*/
/* All rights reserved.                       */

/*********************** self documentation **********************/
/*****************************************************************************
TEMPORARY_FILENAME - Creates a file name in a user-specified directory.

******************************************************************************
Function prototype:
char *temporary_filename(char *tempfile);

******************************************************************************
Input:
tempfile	pointer to directory prefix string (eg. /usr/tmp/)

Output:
tempfile	pointer to filename string (eg. /usr/tmp/1206aaa)

******************************************************************************
Notes:
temporary_filename creates a file name by appending a sequence of
numbers and letters (which is created by tmpnam) to the prefix string
passed as its argument.  On return the input argument points to the
(now augmented) prefix string.

It is duty of the calling program to provide room for the augmented
string.  The resulting string is typically used as a name for a
temporary file; in this case it is the calling program's job to make
sure that the supplied prefix ends with a slash.

This routine was written to supplement the ANSI C function tmpnam
which also creates a temporary filename, but within a fixed directory,
usually the /tmp directory.  Unfortunately, some /tmp directories are
too small to hold typical seismic data sets, so this routine allows
the user to specify a directory with sufficient capacity.  Also note
that on many systems, the tmpfile() call avoids this problem by
simulating a temporary file with a memory buffer.  However, this is
not a panacea as the file size might exceed available memory and on
some systems this call does actually create a file (again, usually in
/tmp).
******************************************************************************
Author:  Jack K. Cohen, Colorado School of Mines, 12/12/95
******************************************************************************/
/**************** end self doc ********************************/

#include "cwp.h"

char *temporary_filename(char *prefix) {
	char buffer[L_tmpnam];
	static char name[BUFSIZ];
	char *tmp = strrchr(tmpnam(buffer), '/');

	strcpy(name, prefix);
	return strcat(name, tmp);
}


#ifdef TEST
main()
{
        FILE *fp;
	char tempfile[BUFSIZ] = "/usr/tmp";

	printf("Temporary filename is %s\n", temporary_filename(tempfile));

	return(EXIT_SUCCESS);
}
#endif
